package com.maimao.auth.config.auth;

import com.maimao.auth.imageCode.email.EmailPwdCodeAuthenticationConfig;
import com.maimao.auth.imageCode.mobile.MobilePwdCodeAuthenticationConfig;
import com.maimao.auth.imageCode.username.UserNameCodeAuthenticationConfig;
import com.maimao.auth.mobilecCode.MobileAuthenticationConfig;
import com.maimao.auth.session.CustomLogoutHandler;
import com.maimao.core.constant.AuthKey;
import com.maimao.core.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

@Configuration
@EnableWebSecurity
@Order(-1)
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
   public RoleHierarchy roleHierarchy() {
        RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
        String separator = System.lineSeparator();
        String hierarchy="ROLE_sys-admin > ROLE_sys-user "+separator+" ROLE_sys-user > ROLE_sys-user-common";
        roleHierarchy.setHierarchy(hierarchy);
        return roleHierarchy;
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                AuthKey.SYS_LOGIN_URL, AuthKey.MOBILE_LOGIN_URL, AuthKey.WEB_LOGIN_URL, AuthKey.WX_LOGIN_URL,
                "/mobile-code", "/image-code", "/web-logout","/sys-logout","/get-jwt",
                "/actuator/**",
                "/instances/**"
        );

    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        AuthenticationManager manager = super.authenticationManagerBean();
        return manager;
    }



    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

  /*  @Autowired
    private InvalidSessionStrategy invalidSessionStrategy;

    *//**
     * 退出清除缓存
     *//*
    @Autowired
    private CustomLogoutHandler customLogoutHandler;
    *//**
     * 当同个用户session数量超过指定值时调用
     *//*
    @Autowired
    private SessionInformationExpiredStrategy sessionInformationExpiredStrategy;

    *//**
     * 为了解决退出重新登录问题
     *
     * @return
     *//*
    @Bean
    public SessionRegistry sessionRegistry() {
        return new SessionRegistryImpl();
    }*/

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
                .httpBasic().and()
                .formLogin()
                .and()
                .authorizeRequests().anyRequest().authenticated();
       /* http.sessionManagement()
                .invalidSessionStrategy(invalidSessionStrategy)
                .maximumSessions(1)
                .expiredSessionStrategy(sessionInformationExpiredStrategy)
                // 当一个用户达到最大session数,则不允许后面再登录)
                .maxSessionsPreventsLogin(true)
                .sessionRegistry(sessionRegistry());
        http.logout().addLogoutHandler(customLogoutHandler)
                .logoutUrl("/sys-logout")
                .deleteCookies(Constants.USER_TOKEN);*/

        http.apply(mobileAuthenticationConfig)
                .and()
                .apply(userNameCodeAuthenticationConfig)
                .and()
                .apply(mobilePwdCodeAuthenticationConfig)
                .and()
                .apply(emailPwdCodeAuthenticationConfig);

    }

    @Autowired
    private MobileAuthenticationConfig mobileAuthenticationConfig;

    @Autowired
    private MobilePwdCodeAuthenticationConfig mobilePwdCodeAuthenticationConfig;
    @Autowired
    private EmailPwdCodeAuthenticationConfig emailPwdCodeAuthenticationConfig;
    @Autowired
    private UserNameCodeAuthenticationConfig userNameCodeAuthenticationConfig;
}
